<template>
  <div class="bms-root">
    <!-- 顶部栏 -->
    <header class="bms-header">
      <div class="bms-logo">五子奕界</div>
      <div class="bms-title">后台管理系统</div>
      <div class="bms-user">
        <img
          class="bms-avatar"
          :src="userStore.avatarUrl(userStore.user.avatar)"
          alt="avatar"
        />
        <span class="bms-username">{{ userStore.user.username }}</span>
        <button class="bms-logout" @click="handLogout()">退出登录</button>
      </div>
    </header>
    <div class="bms-main">
      <!-- 侧边栏 -->
      <div class="bms-sidebar">
        <span class="bms-sidebar-title">菜单</span>
        <ul>
          <li
            :class="{ active: activeMenu === 'home' }"
            @click="activeMenu = 'home'"
          >
            首页
          </li>
          <li
            :class="{ active: activeMenu === 'users' }"
            @click="activeMenu = 'users'"
          >
            用户管理
          </li>
          <li
            :class="{ active: activeMenu === 'records' }"
            @click="activeMenu = 'records'"
          >
            对战数据管理
          </li>
          <li
            :class="{ active: activeMenu === 'personal' }"
            @click="activeMenu = 'personal'"
          >
            个人中心
          </li>
          <!-- 可扩展更多菜单 -->
        </ul>
      </div>
      <!-- 主内容区 -->
      <section class="bms-content">
        <div v-if="activeMenu === 'home'" class="bms-dashboard">
          <h2>欢迎回来，管理员！</h2>
          <div class="dashboard-stats">
            <div class="stat-card">
              <div class="stat-title">用户总数</div>
              <div class="stat-value">{{ userTable.length }}</div>
            </div>
            <div class="stat-card">
              <div class="stat-title">对战总场次</div>
              <div class="stat-value">{{ recordTable.length }}</div>
            </div>
            <div class="stat-card">
              <div class="stat-title">今日新增用户</div>
              <div class="stat-value">{{ newUser }}</div>
            </div>
            <div class="stat-card">
              <div class="stat-title">今日对战场次</div>
              <div class="stat-value">{{ newRecord }}</div>
            </div>
          </div>
          <div class="dashboard-quick">
            <h3>快捷操作</h3>
            <div class="quick-links">
              <button @click="activeMenu = 'users'">用户管理</button>
              <button @click="activeMenu = 'records'">对战数据管理</button>
              <button @click="activeMenu = 'personal'">个人中心</button>
            </div>
          </div>
          <div class="dashboard-announcement">
            <h3>公告</h3>
            <ul>
              <li>欢迎使用五子奕界后台管理系统！</li>
              <li>请及时关注用户活跃情况和对战数据。</li>
            </ul>
          </div>
        </div>
        <UserTable v-if="activeMenu === 'users'" />
        <RecordTable v-if="activeMenu === 'records'" />
        <Personal v-if="activeMenu === 'personal'" />
      </section>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from "vue";
import UserTable from "./userTable.vue";
import RecordTable from "./recordTable.vue";
import Personal from "./personal.vue";
import { useUserStore } from "../../stores/user";
import useNetStore from "../../stores/net.js";

let userStore = useUserStore();
let net = useNetStore();
//  在页面加载完成前，判断用户是否为管理员
onMounted(() => {
if(userStore.user.role !== 1){
  window.location.href = "/";
}
})
// console.log(userStore.user);
const activeMenu = ref("home");
// 退出登录
function handLogout() {
  if (window.confirm("确认退出登录吗？")) {
    net
      .fasong(
        {
          userId: userStore.user.userId,
        },
        "/users/logout"
      )
      .then((res) => {
        //   console.log(res);
        if (res.code === 200) {
          console.log("退出登录成功");
          userStore.logout();
          // 跳转到登录页面
        } else {
          console.log(res.msg);
        }
      })
      .catch((err) => {
        console.log(err);
      });
  }
}
// 获取用户列表
let userTable = ref([]);
net
  .get("/users/userTable")
  .then((res) => {
    if (res.code === 200) {
      userTable.value = res.data;
      // console.log(userTable.value)
      console.log(res.msg || "获取用户列表成功");
    } else {
      console.log(res.msg || "获取用户列表失败");
    }
  })
  .catch((err) => {
    console.error(err);
  });

// 获取对战数据列表
let recordTable = ref([]);
net
  .get("/users/recordTable")
  .then((res) => {
    if (res.code === 200) {
      recordTable.value = res.data;
      // console.log(recordTable.value)
      console.log(res.msg || "获取用户列表成功");
    } else {
      console.log(res.msg || "获取用户列表失败");
    }
  })
  .catch((err) => {
    console.error(err);
  });
// 今日新增用户
let newUser = ref(0);
// 今日对战场次
let newRecord = ref(0);
// 获取今日新增用户
// 等到数据加载完成后再获取
onMounted(() => {
  setTimeout(() => {
    for (let i = 0; i < userTable.value.length; i++) {
      let date = userTable.value[i].createdAt;
      let formatTime = formatDate(date);
      if (formatTime) {
        newUser.value += 1;
      }
    }
    for (let i = 0; i < recordTable.value.length; i++) {
      let date = recordTable.value[i].end_time;
      // console.log(date); //2025-06-17T16:35:40.000Z
      let formatTime = formatDate(date);
      // console.log(formatTime);
      if (formatTime) {
        newRecord.value += 1;
      }
      // console.log(newUser.value,newRecord.value)
    }
  }, 100);
});

function formatDate(sj) {
  let formatTime = 0;
  let date = new Date(sj);
  date =
    date.getFullYear() +
    "-" +
    String(date.getMonth() + 1).padStart(2, "0") +
    "-" +
    String(date.getDate()).padStart(2, "0") +
    " " +
    String(date.getHours()).padStart(2, "0") +
    ":" +
    String(date.getMinutes()).padStart(2, "0") +
    ":" +
    String(date.getSeconds()).padStart(2, "0");
  date = date.replace(/-/g, "/").replace("T", " ").replace(".000Z", "");
  const time = new Date(date);
  const year = time.getFullYear();
  const month = time.getMonth() + 1;
  const day = time.getDate();
  // console.log(year, month, day);
  // 如果是今天
  const now = new Date();
  const nowYear = now.getFullYear();
  const nowMonth = now.getMonth() + 1;
  const nowDay = now.getDate();
  // console.log(nowYear, nowMonth, nowDay);
  if (year === nowYear && month === nowMonth && day === nowDay) {
    // 今天
    formatTime = 1;
  } else {
    formatTime = 0;
  }
  return formatTime;
}
</script>

<style scoped>
.bms-root {
  width: 100vw;
  max-width: calc(100vw - 24px);
  min-height: 100vh;
  background: linear-gradient(120deg, #f7f7fa 60%, #e6eaf3 100%);
  overflow:auto;
}

.bms-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 64px;
  background: #fff;
  border-bottom: 2px solid #e0e6ed;
  padding: 0 36px;
}

.bms-logo {
  font-size: 1.6rem;
  font-weight: bold;
  color: #2056b3;
  letter-spacing: 2px;
}

.bms-title {
  font-size: 2rem;
  font-weight: 700;
  color: #2056b3;
  letter-spacing: 2px;
}

.bms-user {
  display: flex;
  align-items: center;
  gap: 12px;
}

.bms-avatar {
  width: 38px;
  height: 38px;
  border-radius: 50%;
  border: 2px solid #e0e6ed;
  background: #fff;
}

.bms-username {
  font-size: 1.1rem;
  color: #2056b3;
  font-weight: 600;
}

.bms-logout {
  background: #ff4d4f;
  color: #fff;
  border: none;
  border-radius: 8px;
  padding: 6px 18px;
  font-size: 1rem;
  cursor: pointer;
  margin-left: 8px;
  transition: all 0.2s;
}

.bms-logout:hover {
  background: #d9363e;
}

.bms-main {
  display: flex;
  min-height: calc(100vh - 64px);
}

.bms-sidebar {
  width: 220px;
  background: rgba(255, 255, 255, 0.95);
  border-right: 2px solid #e0e6ed;
  padding: 36px 0 0 0;
  display: flex;
  flex-direction: column;
  align-items: center;
}
.bms-sidebar-title {
  font-size: 1.2rem;
  color: #2056b3;
  margin-bottom: 24px;
  text-align: center;
  background: #fff;
}
.bms-sidebar ul {
  width: 90%;
  list-style: none;
  padding: 0;
  margin: 0;
}

.bms-sidebar li {
  font-size: 1.2rem;
  color: #2056b3;
  padding: 18px 24px;
  margin-bottom: 12px;
  border-radius: 8px;
  cursor: pointer;
  background: #f7f7fa;
  transition: background 0.2s, color 0.2s;
  border: 2px solid transparent;
  text-align: left;
}

.bms-sidebar li.active,
.bms-sidebar li:hover {
  background: #e6eaf3;
  color: #2056b3;
  border: 2px solid #2056b3;
}

.bms-content {
  flex: 1;
  padding: 48px 48px 0 48px;
  min-height: 600px;
  overflow: auto;
}

.bms-dashboard {
  text-align: center;
  margin-top: 40px;
}

.dashboard-stats {
  display: flex;
  justify-content: center;
  gap: 32px;
  margin: 32px 0;
  flex-wrap: wrap;
}

.stat-card {
  background: #f7f7fa;
  border-radius: 12px;
  box-shadow: 0 2px 8px rgba(60, 60, 60, 0.06);
  padding: 28px 38px;
  min-width: 160px;
  margin-bottom: 16px;
}

.stat-title {
  font-size: 1.1rem;
  color: #2056b3;
  margin-bottom: 10px;
}

.stat-value {
  font-size: 2.2rem;
  color: #2056b3;
  font-weight: bold;
}

.dashboard-quick {
  margin: 36px 0 24px 0;
}

.dashboard-quick h3 {
  font-size: 1.2rem;
  color: #2056b3;
  margin-bottom: 16px;
}

.quick-links {
  display: flex;
  justify-content: center;
  gap: 24px;
}

.quick-links button {
  padding: 10px 32px;
  font-size: 1.1rem;
  border: none;
  border-radius: 8px;
  background: #bfc8d8;
  color: #333;
  cursor: pointer;
  transition: background 0.2s;
}

.quick-links button:hover {
  background: #a5b1c2;
}

.dashboard-announcement {
  margin-top: 32px;
  text-align: left;
  max-width: 600px;
  margin-left: auto;
  margin-right: auto;
}

.dashboard-announcement h3 {
  color: #2056b3;
  font-size: 1.1rem;
  margin-bottom: 10px;
}

.dashboard-announcement ul {
  padding-left: 20px;
  color: #2056b3;
  font-size: 1rem;
}
</style>
